package top.bulk.service.impl;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import top.bulk.entity.User;
import top.bulk.mapper.db1.UserMapper;
import top.bulk.service.UserService;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * impl
 *
 * @author 散装java
 * @version 1.0.0
 * @date 2025-02-15
 */
@Service
@Slf4j
public class UserServiceImpl implements UserService {

    @Resource
    private UserMapper userMapper;

    @Override
    public void insert(User user) {
        userMapper.insert(user);
    }

    @Override
    public List<User> queryListLatest() {
        return userMapper.queryListLatest();
    }

    @Override
    // @Transactional(propagation = Propagation.REQUIRES_NEW)
    @Transactional
    public void nestedErrTx() {
        log.info("nestedErrTx 处理");
        String dateStr = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
        User user = new User("内层User", dateStr);
        userMapper.insert(user);
        log.info("nestedErrTx user:{}", user);

        throw new RuntimeException("nestedErrTx 模拟异常");
    }
}
